module(...,package.seeall)

local storyboard = require( "storyboard" )
	local scene = storyboard.newScene()
	local ui = require ("ui")
	local widget = require "widget"
	local wEasing = require "wEasing"
	local gui = require "Home.HomeGui".new()
function scene:createScene(event)
	local mainHomeGroup = self.view
	local bg = display.newImageRect(mainHomeGroup, gui.bg.fn, gui.bg.w, gui.bg.h)
		bg.x = gui.bg.x
		bg.y = gui.bg.y
	local gr = display.newGroup()
	local arrayButtonTouch = {}
	function viewRank()
		local g1 = require "Home.popupRank".new()
			local group1 = display.newGroup()
				group1:insert(g1)
				mainHomeGroup:insert(group1)
				group1.y = -300
				transition.to(group1,{time = 200,alpha = 1, x = 0, y = 0, transition = wEasing.easeOutElastic})
	end
	function shareFacebook()
		local g1 = require "Home.popupFacebook".new()
			local group1 = display.newGroup()
			group1:insert(g1)
			mainHomeGroup:insert(group1)
			group1.y = -300
			transition.to(group1,{time = 200,alpha = 1, x = 0, y = 0, transition = wEasing.easeOutElastic})
	end
	function setting()
		local g1 = require "Home.popupSetting".new()
			local group1 = display.newGroup()
			group1:insert(g1)
			mainHomeGroup:insert(group1)
			group1.y = -300
			transition.to(group1,{time = 200,alpha = 1, x = 0, y = 0, transition = wEasing.easeOutElastic})
	end
	local btnRank = ui.newButton({defaultSrc = gui.btnRank.fn,
									overSrc = gui.btnRank.fn,
									defaultX = gui.btnRank.w,
									defaultY = gui.btnRank.h,
									overX = gui.btnRank.w,
									overY = gui.btnRank.h,
									overAlpha = gui.btnRank.overAlpha,
									overScale = gui.btnRank.overScale,
									x = gui.btnRank.x,
									y = gui.btnRank.y,
									onRelease = viewRank
								})
		mainHomeGroup:insert(btnRank)
	local btnFacebook = ui.newButton({
										defaultSrc = gui.btnFacebook.fn,
										overSrc = gui.btnFacebook.fn,
										defaultX = gui.btnFacebook.w,
										defaultY = gui.btnFacebook.h,
										overX = gui.btnFacebook.w,
										overY = gui.btnFacebook.h,
										overAlpha = gui.btnFacebook.overAlpha,
										overScale = gui.btnFacebook.overScale,
										x = gui.btnFacebook.x,
										y = gui.btnFacebook.y,
										onRelease = shareFacebook
									})
		mainHomeGroup:insert(btnFacebook)
	
	local btnSetting = ui.newButton({
										defaultSrc = gui.btnSetting.fn,
										overSrc = gui.btnSetting.fn,
										defaultX = gui.btnSetting.w,
										defaultY = gui.btnSetting.h,
										overX = gui.btnSetting.w,
										overY = gui.btnSetting.h,
										overAlpha = gui.btnSetting.overAlpha,
										overScale = gui.btnSetting.overScale,
										x = gui.btnSetting.x,
										y = gui.btnSetting.y,
										onRelease = setting
									})
		mainHomeGroup:insert(btnSetting)
	local groupCandy1 = display.newGroup()
	local groupCandy2 = display.newGroup()
	local groupCandy3 = display.newGroup()
	local candy11 = display.newImageRect(mainHomeGroup, gui.candy11.fn, gui.candy11.w, gui.candy11.h)
		candy11.x = gui.candy11.x
		candy11.y = gui.candy11.y
		candy11:setFillColor (0,0,255)
		groupCandy1:insert(candy11)
	local candy12 = display.newImageRect(mainHomeGroup, gui.candy12.fn, gui.candy12.w, gui.candy12.h)
		candy12.x = gui.candy12.x
		candy12.y = gui.candy12.y
		candy12:setFillColor(255,0,0)
		groupCandy1:insert(candy12)
	local candy13 = display.newImageRect(mainHomeGroup, gui.candy13.fn, gui.candy13.w, gui.candy13.h)
		candy13.x = gui.candy13.x
		candy13.y = gui.candy13.y
		candy13:setFillColor (0,0,255)
		groupCandy1:insert(candy13)
	local candy14 = display.newImageRect(mainHomeGroup, gui.candy14.fn, gui.candy14.w, gui.candy14.h)
		candy14.x = gui.candy14.x
		candy14.y = gui.candy14.y
		candy14:setFillColor(255,0,0)
		groupCandy1:insert(candy14)
	local candy15 = display.newImageRect(mainHomeGroup, gui.candy15.fn, gui.candy15.w, gui.candy15.h)
		candy15.x = gui.candy15.x
		candy15.y = gui.candy15.y
		candy15:setFillColor (0,0,255)
		groupCandy1:insert(candy15)
	local candy16 = display.newImageRect(mainHomeGroup, gui.candy16.fn, gui.candy16.w, gui.candy16.h)
		candy16.x = gui.candy16.x
		candy16.y = gui.candy16.y
		candy16:setFillColor(255,0,0)
		groupCandy1:insert(candy16)
	local circle = display.newImageRect(mainHomeGroup, gui.circle.fn, gui.circle.w, gui.circle.h)
		circle.x = gui.circle.x
		circle.y = gui.circle.y
		groupCandy1:insert(circle)
		-------------------------------------------------------------------------------------------
	local candy21 = display.newImageRect(mainHomeGroup, gui.candy21.fn, gui.candy21.w, gui.candy21.h)
		candy21.x = gui.candy21.x
		candy21.y = gui.candy21.y
		candy21:setFillColor (0,0,255)
		groupCandy2:insert(candy21)
	local candy22 = display.newImageRect(mainHomeGroup, gui.candy22.fn, gui.candy22.w, gui.candy22.h)
		candy22.x = gui.candy22.x
		candy22.y = gui.candy22.y
		candy22:setFillColor(255,0,0)
		groupCandy2:insert(candy22)
	local candy23 = display.newImageRect(mainHomeGroup, gui.candy23.fn, gui.candy23.w, gui.candy23.h)
		candy23.x = gui.candy23.x
		candy23.y = gui.candy23.y
		candy23:setFillColor (0,0,255)
		groupCandy2:insert(candy23)
	local candy24 = display.newImageRect(mainHomeGroup, gui.candy24.fn, gui.candy24.w, gui.candy24.h)
		candy24.x = gui.candy24.x
		candy24.y = gui.candy24.y
		candy24:setFillColor(255,0,0)
		groupCandy2:insert(candy24)
	local candy25 = display.newImageRect(mainHomeGroup, gui.candy25.fn, gui.candy25.w, gui.candy25.h)
		candy25.x = gui.candy25.x
		candy25.y = gui.candy25.y
		candy25:setFillColor (0,0,255)
		groupCandy2:insert(candy25)
	local candy26 = display.newImageRect(mainHomeGroup, gui.candy26.fn, gui.candy26.w, gui.candy26.h)
		candy26.x = gui.candy26.x
		candy26.y = gui.candy26.y
		candy26:setFillColor(255,0,0)
		groupCandy2:insert(candy26)
	local circle = display.newImageRect(mainHomeGroup, gui.circle.fn, gui.circle.w, gui.circle.h)
		circle.x = gui.circle.x
		circle.y = gui.circle.y
		groupCandy2:insert(circle)
	------------------------------------------------------------------------------------------------
	local candy31 = display.newImageRect(mainHomeGroup, gui.candy31.fn, gui.candy31.w, gui.candy31.h)
		candy31.x = gui.candy31.x
		candy31.y = gui.candy31.y
		candy31:setFillColor (0,0,255)
		groupCandy3:insert(candy31)
	local candy32 = display.newImageRect(mainHomeGroup, gui.candy32.fn, gui.candy32.w, gui.candy32.h)
		candy32.x = gui.candy32.x
		candy32.y = gui.candy32.y
		candy32:setFillColor(255,0,0)
		groupCandy3:insert(candy32)
	local candy33 = display.newImageRect(mainHomeGroup, gui.candy33.fn, gui.candy33.w, gui.candy33.h)
		candy33.x = gui.candy33.x
		candy33.y = gui.candy33.y
		candy33:setFillColor (0,0,255)
		groupCandy3:insert(candy33)
	local candy34 = display.newImageRect(mainHomeGroup, gui.candy34.fn, gui.candy34.w, gui.candy34.h)
		candy34.x = gui.candy34.x
		candy34.y = gui.candy34.y
		candy34:setFillColor(255,0,0)
		groupCandy3:insert(candy34)
	local candy35 = display.newImageRect(mainHomeGroup, gui.candy35.fn, gui.candy35.w, gui.candy35.h)
		candy35.x = gui.candy35.x
		candy35.y = gui.candy35.y
		candy35:setFillColor (0,0,255)
		groupCandy3:insert(candy35)
	local candy36 = display.newImageRect(mainHomeGroup, gui.candy36.fn, gui.candy36.w, gui.candy36.h)
		candy36.x = gui.candy36.x
		candy36.y = gui.candy36.y
		candy36:setFillColor(255,0,0)
		groupCandy3:insert(candy36)
	local circle = display.newImageRect(mainHomeGroup, gui.circle.fn, gui.circle.w, gui.circle.h)
		circle.x = gui.circle.x
		circle.y = gui.circle.y
		groupCandy3:insert(circle)
	
	arrayButtonTouch[1] = display.newImageRect(mainHomeGroup, gui.buttonTouch.fn, gui.buttonTouch.w, gui.buttonTouch.h)
	arrayButtonTouch[1].x = gui.buttonTouch.x
	arrayButtonTouch[1].y = gui.buttonTouch.y
	arrayButtonTouch[2] = display.newImageRect(mainHomeGroup, gui.buttonTouch.fn, gui.buttonTouch.w, gui.buttonTouch.h)
	arrayButtonTouch[2].x = gui.buttonTouch.x+80
	arrayButtonTouch[2].y = gui.buttonTouch.y+60
	arrayButtonTouch[3] = display.newImageRect(mainHomeGroup, gui.buttonTouch.fn, gui.buttonTouch.w, gui.buttonTouch.h)
	arrayButtonTouch[3].x = gui.buttonTouch.x+70
	arrayButtonTouch[3].y = gui.buttonTouch.y+160
	arrayButtonTouch[4] = display.newImageRect(mainHomeGroup, gui.buttonTouch.fn, gui.buttonTouch.w, gui.buttonTouch.h)
	arrayButtonTouch[4].x = gui.buttonTouch.x-20
	arrayButtonTouch[4].y = gui.buttonTouch.y +210
	arrayButtonTouch[5] = display.newImageRect(mainHomeGroup, gui.buttonTouch.fn, gui.buttonTouch.w, gui.buttonTouch.h)
	arrayButtonTouch[5].x = gui.buttonTouch.x - 100
	arrayButtonTouch[5].y = gui.buttonTouch.y +150
	arrayButtonTouch[6] = display.newImageRect(mainHomeGroup, gui.buttonTouch.fn, gui.buttonTouch.w, gui.buttonTouch.h)
	arrayButtonTouch[6].x = gui.buttonTouch.x - 90
	arrayButtonTouch[6].y = gui.buttonTouch.y +50
	---------------------------------------------------------------------------------------------------------------
	arrayButtonTouch[7] = display.newImageRect(mainHomeGroup, gui.buttonTouch.fn, gui.buttonTouch.w, gui.buttonTouch.h)
	arrayButtonTouch[7].x = gui.buttonTouch.x
	arrayButtonTouch[7].y = gui.buttonTouch.y
	arrayButtonTouch[8] = display.newImageRect(mainHomeGroup, gui.buttonTouch.fn, gui.buttonTouch.w, gui.buttonTouch.h)
	arrayButtonTouch[8].x = gui.buttonTouch.x+80
	arrayButtonTouch[8].y = gui.buttonTouch.y+60
	arrayButtonTouch[9] = display.newImageRect(mainHomeGroup, gui.buttonTouch.fn, gui.buttonTouch.w, gui.buttonTouch.h)
	arrayButtonTouch[9].x = gui.buttonTouch.x+70
	arrayButtonTouch[9].y = gui.buttonTouch.y+160
	arrayButtonTouch[10] = display.newImageRect(mainHomeGroup, gui.buttonTouch.fn, gui.buttonTouch.w, gui.buttonTouch.h)
	arrayButtonTouch[10].x = gui.buttonTouch.x-20
	arrayButtonTouch[10].y = gui.buttonTouch.y +210
	arrayButtonTouch[11] = display.newImageRect(mainHomeGroup, gui.buttonTouch.fn, gui.buttonTouch.w, gui.buttonTouch.h)
	arrayButtonTouch[11].x = gui.buttonTouch.x - 100
	arrayButtonTouch[11].y = gui.buttonTouch.y +150
	arrayButtonTouch[12] = display.newImageRect(mainHomeGroup, gui.buttonTouch.fn, gui.buttonTouch.w, gui.buttonTouch.h)
	arrayButtonTouch[12].x = gui.buttonTouch.x - 90
	arrayButtonTouch[12].y = gui.buttonTouch.y +50
	---------------------------------------------------------------------------------------------------------------------
	arrayButtonTouch[13] = display.newImageRect(mainHomeGroup, gui.buttonTouch.fn, gui.buttonTouch.w, gui.buttonTouch.h)
	arrayButtonTouch[13].x = gui.buttonTouch.x
	arrayButtonTouch[13].y = gui.buttonTouch.y
	arrayButtonTouch[14] = display.newImageRect(mainHomeGroup, gui.buttonTouch.fn, gui.buttonTouch.w, gui.buttonTouch.h)
	arrayButtonTouch[14].x = gui.buttonTouch.x+80
	arrayButtonTouch[14].y = gui.buttonTouch.y+60
	arrayButtonTouch[15] = display.newImageRect(mainHomeGroup, gui.buttonTouch.fn, gui.buttonTouch.w, gui.buttonTouch.h)
	arrayButtonTouch[15].x = gui.buttonTouch.x+70
	arrayButtonTouch[15].y = gui.buttonTouch.y+160
	arrayButtonTouch[16] = display.newImageRect(mainHomeGroup, gui.buttonTouch.fn, gui.buttonTouch.w, gui.buttonTouch.h)
	arrayButtonTouch[16].x = gui.buttonTouch.x-20
	arrayButtonTouch[16].y = gui.buttonTouch.y +210
	arrayButtonTouch[17] = display.newImageRect(mainHomeGroup, gui.buttonTouch.fn, gui.buttonTouch.w, gui.buttonTouch.h)
	arrayButtonTouch[17].x = gui.buttonTouch.x - 100
	arrayButtonTouch[17].y = gui.buttonTouch.y +150
	arrayButtonTouch[18] = display.newImageRect(mainHomeGroup, gui.buttonTouch.fn, gui.buttonTouch.w, gui.buttonTouch.h)
	arrayButtonTouch[18].x = gui.buttonTouch.x - 90
	arrayButtonTouch[18].y = gui.buttonTouch.y +50
	for i = 1,18 do
		if i >0 and i <=6 then
			groupCandy1:insert(arrayButtonTouch[i])
			arrayButtonTouch[i].alpha = 1
		elseif i >6 and i <=12 then
			groupCandy2:insert(arrayButtonTouch[i])
			arrayButtonTouch[i].alpha = 1
			arrayButtonTouch[i]:setFillColor(0,0,255)
		elseif i>12 and i <= 18 then
			groupCandy3:insert(arrayButtonTouch[i])
			arrayButtonTouch[i].alpha = 1
			arrayButtonTouch[i]:setFillColor(255,0,0)
		end
		arrayButtonTouch[i].id = i
	end
	function onTapPlay(event)
		t = event.target
		--for i = 1,18 do
			if t.id == 1 then
				storyboard.gotoScene( "Stage1.Stage1Home", "fade", 10 )
			end
			if t.id == 2 then
				storyboard.gotoScene( "Stage2.Stage2Home", "fade", 10 )
			end
			if t.id == 3 then
				storyboard.gotoScene( "Stage3.Stage3Home", "fade", 10 )
			end
			if i == 4 then
				storyboard.gotoScene( "Stage4.Stage4Home", "fade", 10 )
			end
			if t.id == 5 then
				storyboard.gotoScene( "Stage5.Stage5Home", "fade", 10 )
			end
			if t.id == 6 then
				storyboard.gotoScene( "Stage6.Stage6Home", "fade", 10 )
			end
			if t.id == 7 then
				storyboard.gotoScene( "Stage7.Stage7Home", "fade", 10 )
			end
			if t.id == 8 then
				storyboard.gotoScene( "Stage8.Stage8Home", "fade", 10 )
			end
			if t.id == 9 then
				storyboard.gotoScene( "Stage9.Stage9Home", "fade", 10 )
			end
			if t.id == 10 then
				storyboard.gotoScene( "Stage10.Stage10Home", "fade", 10 )
			end
			if t.id == 11 then
				storyboard.gotoScene( "Stage11.Stage11Home", "fade", 10 )
			end
			if t.id == 12 then
				storyboard.gotoScene( "Stage12.Stage12Home", "fade", 10 )
			end
			if t.id == 13 then
				storyboard.gotoScene( "Stage13.Stage13Home", "fade", 10 )
			end
			if t.id == 14 then
				storyboard.gotoScene( "Stage14.Stage14Home", "fade", 10 )
			end
			if t.id == 15 then
				storyboard.gotoScene( "Stage15.Stage15Home", "fade", 10 )
			end
			if t.id == 16 then
				storyboard.gotoScene( "Stage16.Stage16Home", "fade", 10 )
			end
			if t.id == 17 then
				storyboard.gotoScene( "Stage17.Stage17Home", "fade", 10 )
			end
			if t.id == 18 then
				storyboard.gotoScene( "Stage18.Stage18Home", "fade", 10 )
			end
		--end
	end
	for i = 1,18 do
		arrayButtonTouch[i]:addEventListener("tap", onTapPlay)
	end
	function scrollListener(event)
		phase = event.phase
		if phase == "began" then
			--print (event.x)
		elseif phase == "moved" then
			
		elseif phase == "ended" then
			-- local a, b = scrvGame:getContentPosition()
			-- print(a)
			-- print(b)
			--print(scrvGame.x)
		end
		return true
	end
	local scrvGame = widget.newScrollView{
        				left = gui.scrvGame.l, 
						top = gui.scrvGame.t, 
						width = gui.scrvGame.w,------chiều rộng của scrollview
						height = gui.scrvGame.h,--chiều cao của scrollview
						friction = gui.scrvGame.friction,
                        horizontalScrollDisabled = false,--cho phép cuộn theo chiều ngang
						verticalScrollDisabled = true,--ko cho phép cuộn theo chiều dọc
                        backgroundColor = {0,0,0,180},
                        hideBackground = true,--cho phép hiển thị background
						hideScrollBar = false,
						listener = scrollListener,
						maskFile = gui.scrvGame.mask,----------tùy chỉnh kích thước scrollview theo mask
						}	
		gr:insert(scrvGame)		
		mainHomeGroup:insert(gr)
			scrvGame:insert(groupCandy1)
			groupCandy1.x = -gui.scrvGame.l-1
			groupCandy1.y =  -gui.scrvGame.t+10
			scrvGame:insert(groupCandy2)
			groupCandy2.x = -gui.scrvGame.l-1 +1*300
			groupCandy2.y = -gui.scrvGame.t+10
			scrvGame:insert(groupCandy3)
			groupCandy3.x = -gui.scrvGame.l-1 +2*300
			groupCandy3.y = -gui.scrvGame.t+10
		--print(groupCandy1.x)
		
	return mainHomeGroup
end
-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
	local group = self.view
	local prior_scene = storyboard.getPrevious()
    storyboard.purgeScene( prior_scene )
	storyboard.printMemUsage()
	-- audio.play( sfx.themeMusic, { onComplete = function() 
                                      -- audio.dispose( sfx.themeMusic ) 
                                      -- end } )
	-----------------------------------------------------------------------------
		
	--	INSERT code here (e.g. start timers, load audio, start listeners, etc.)
	
	-----------------------------------------------------------------------------
	
end


-- Called when scene is about to move offscreen:
function scene:exitScene( event )
	local group = self.view
	
	-----------------------------------------------------------------------------
	
	--	INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)
	
	-----------------------------------------------------------------------------
	
end


-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )
	local group = self.view
	
	-----------------------------------------------------------------------------
	
	--	INSERT code here (e.g. remove listeners, widgets, save state, etc.)
	
	-----------------------------------------------------------------------------
	
end

---------------------------------------------------------------------------------
-- END OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------

-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )

-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )

-- "exitScene" event is dispatched before next scene's transition begins
scene:addEventListener( "exitScene", scene )

-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )

---------------------------------------------------------------------------------

return scene
